package cn.zjh.org.service.impl;

import cn.zjh.user.service.service.impl.BaseServiceImpl;
import cn.zjh.org.domain.Department;
import cn.zjh.org.mapper.DepartmentMapper;
import cn.zjh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {


    @Autowired
    private DepartmentMapper departmentMapper;





    /**
    * @Title: getTree
    * @Description: 获取部门树数据
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/26 14:36
    * @Parameters:
    * @Return
    */
    @Override
    public List<Department> getTree() {
        //查询所有数据
        List<Department> deptAll = departmentMapper.loadAll();

        ArrayList<Department> deptTree = new ArrayList<>();

        HashMap<Long, Department> deptMap = new HashMap<>();

        //遍历集合对象，将所有部门对象存入map
        for(Department depts : deptAll){
            deptMap.put(depts.getId(),depts);
        }

        //遍历部门对象
        for (Department deptTem : deptAll) {
            //如果部门对象的父类id为null，那他就是顶级部门
            if (deptTem.getParentId() == null){
                //将此对象存入list集合
                deptTree.add(deptTem);
            }else{
                //从map集合中获取父类对象
                Department department = deptMap.get(deptTem.getParentId());
                //如果获取的父类对象不为空
                if (department!=null){
                    //将对应子类对象设置到父类当中
                    department.getChildren().add(deptTem);
                }
            }
        }
        return deptTree ;
    }


    /**
    * @Title: save
    * @Description: 新增
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/26 22:43
    * @Parameters:
    * @Return
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        //设置空字符串用于拼接路径
        String dirPath = "";

        //如果当前部门对象的parentid为null说明是顶级部门
        if (department.getParent().getId() == null){
            //那么dirPath就是/+自身的id
            dirPath = "/"+department.getId();
        }else {
            //否则此对象就有上级部门，我们找到他的上级部门
            Department deptParent = loadById(department.getParent().getId());
            //如果上级部门不为空
            if (deptParent!=null){
                //那么dirPath就是上级部门对象的dirPath+"/"+自身的id
                dirPath = deptParent.getDirPath()+"/"+department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }




    /**
    * @Title: update
    * @Description: 修改
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/26 22:43
    * @Parameters:
    * @Return
    */
    @Override
    @Transactional
    public void update(Department department) {
        //设置空字符串用于拼接路径
        String dirPath = "";

        //如果当前部门对象的parentid为null说明是顶级部门
        if (department.getParent().getId() == null){
            //那么dirPath就是/+自身的id
            dirPath = "/"+department.getId();
        }else {
            //否则此对象就有上级部门，我们找到他的上级部门
            Department deptParent = loadById(department.getParent().getId());
            //如果上级部门不为空
            if (deptParent != null){
                //那么dirPath就是上级部门对象的dirPath+"/"+自身的id
                dirPath = deptParent.getDirPath()+"/"+department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }





}
